<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\UserModel;
use app\admin\model\RoleModel;

class User extends Base
{
	 /**
	 * 用户对象
	 * @return mixed
	 */
	public $user_obj;
	
	/**
	 * 权限对象
	 * @return mixed
	 */
	public $role_obj;
	
	/**
	 * 加密token
	 * @return mixed
	 */
	public $token;
	 /**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->user_obj = new UserModel(); 
		$this->role_obj = new RoleModel(); 	
		$this->token = TOKEN; 	
	}
	 /**
	 * 用户列表
	 * @return mixed
	 */
	public function index()
    {
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->user_obj->getUserList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$role = RoleModel::get($val['role_id']);
					$data['list']['data'][$key]['role_name'] = $role->name ? $role->name : '';
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			return view('index');
		}
    }
	 /**
	 * 新增用户页面
	 * @return mixed
	 */
	public function add()
	{
		$this->assign('role', $this->role_obj->getRoleList());
		return view('add');
	}
	/**
	 * 编辑用户
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = UserModel::get($input['id'])->toArray();
		$this->assign('view', $view);
		$this->assign('role', $this->role_obj->getRoleList());
		return view('edit');
	}
	/**
	 * 更新用户
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['password'])){
				$input['password'] = md5($this->token.$input['password']);
			}
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->user_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->user_obj->data($input);
				$flag = $this->user_obj->save();
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新用户数据，请求不合法！');
		}
	}
	/**
	 * 删除用户
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$user = UserModel::get($input['id']);
			$user->status = -1;
			$flag = $user->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新用户数据，请求不合法！');
		}
	}
	
	/**
	 * 更新用户状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$user = UserModel::get($input['id']);
			$status = 1;
			if($user->status){
				$status = 0;
			}
			$user->status = $status;
			$flag = $user->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新用户数据，请求不合法！');
		}
	}
	
}
